package com.controller;

import com.bean.dto.Dto;
import com.bean.entity.Client;
import com.bean.vo.ClientVo;
import com.bean.vo.PageAssistant;
import com.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 客户信息页面
 */
@RestController
@RequestMapping("c")
public class ClientController {
    /* 自动注入 */
    @Autowired
    ClientService clientService;

    /* 查询分类、条件 */
    @RequestMapping("queryAll")
    public Dto queryAll(ClientVo clientVo,PageAssistant pa){
        //查询
        System.out.println(">>> 条件、分页查询");
        //创建默认对象
        PageAssistant<ClientVo> pa1 = new PageAssistant<>();
        ClientVo clientVo1 = new ClientVo();
        //判断分页
        if(pa.getPageSize() != 5){
            pa1.setPageSize(pa.getPageSize());
        }
        if(pa.getCurrPage() != 1){
            pa1.setCurrPage(pa.getCurrPage());
        }
        if(clientVo != null){
            return new Dto("0","查询",clientService.queryAll(clientVo,pa1),"成功!");
        }else{
            return new Dto("0","查询",clientService.queryAll(clientVo1,pa1),"成功!");
        }
    }

    @RequestMapping("query")
    public Dto query(Client client){
        //查询单个
        System.out.println(">>> 详细信息");
        //执行
        try {
            ClientVo clientVo = clientService.query(client);
            return new Dto("0","查询详细信息",clientVo,"成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Dto("0",e.getMessage(),null,"失败!");
        }
    }

    @RequestMapping("save")
    public Dto save(Client client){
        //添加
        System.out.println(">>> 添加客户信息");

        try {
            clientService.save(client);
            return new Dto("0","新增成功!",null,"成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Dto("0","新增失败!",null,"失败!");
        }
    }

    @RequestMapping("remove")
    public Dto remove(Client client){
        //删除
        System.out.println(">>> 删除客户信息");

        try {
            clientService.remove(client);
            return new Dto("0","删除成功!",null,"成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Dto("0","删除失败!",null,"失败!");
        }
    }

    @RequestMapping("modify")
    public Dto modify(Client client){
        //删除
        System.out.println(">>> 修改客户信息");

        try {
            clientService.modify(client);
            return new Dto("0","修改成功!",null,"成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Dto("0","修改失败!",null,"失败!");
        }
    }
}
